<?php
//专题列表
if(!defined('IN_T')){
   die('hacking attempt');
}

$act = Common::sfilter($_REQUEST['act']);

//删除专题
if($act=='delete'){
	$id = intval($_REQUEST['id']);
	$Db->delete($Base->table('topic'),array('tid'=>$id));
	$Db->delete($Base->table('topic_cat'),array('id'=>$id));
	echo $Json->encode(array('status'=>1));
	exit;
}
//编辑、添加
else if($act=='edit'){
	$tid = intval($_REQUEST['tid']);
	if($tid>0){
		$t = $Db->query("select * from ".$Base->table('topic_cat')." where id=$tid",'Row');
	}
	if(!$t){
		$tid = 0;
	}
	//显示界面 
	if(empty($_POST)){
		$tp->assign('t',$t);
	}
	//执行操作
	else{
		$data = array(
			'name'=>Common::sfilter($_POST['name']),
			'brief'=>Common::sfilter($_POST['brief']),
			'bottom'=>Common::sfilter($_POST['bottom']),
		);
		$res['status'] = 0;
		if(empty($data)){
			$res['msg'] = '请填写专题名称';
		}
		else if(empty($data['brief'])){
			$res['msg'] = '请填写专题描述';
		}
		else{
			$res = array('status'=>1,'href'=>'/'.ADMIN_PATH.'/?m=topiccat');
			//编辑
			if($tid>0){
				$Db->update($Base->table('topic_cat'),$data,array('id'=>$tid));
				$res['msg'] = '编辑成功';
			}
			//添加
			else{
				$Db->insert($Base->table('topic_cat'),$data);
				$res['msg'] = '添加成功';
			}
		}
		echo $Json->encode($res);
		exit;
	}
}
//提取列表
else{
	$size = 15; //定义每页显示10条
	$page = intval($_REQUEST['page']);
	$page = $page<1 ? 1 : $page;
	$res = get_topic_cats($page,$size);
	$pages = Common::set_page($size,$page,$res['count']);
	foreach ($pages as $key => $value) {
		$pages[$key]['url'] = "/".ADMIN_PATH."/?m=topiccat&page=".$value['num'];
	}
	$tp->assign("page",$page);
	$tp->assign("pages",$pages);
	$tp->assign("res",$res);
}
$tp->assign('nav','专题管理');
$tp->assign('act',$act);

//提取全景项目
function get_topic_cats($page,$size){
	$sql = "SELECT * FROM ".$GLOBALS['Base']->table('topic_cat')." WHERE 1 ";
	if(!empty($name)){
		$sql .=" AND name LIKE '%$name%'";
	}
	$res['count'] = count($GLOBALS['Db']->query($sql));
	$sql .= "order by id desc limit ".($page-1)*$size.", ".$size."";
	//echo $sql;
	$res['res'] = $GLOBALS['Db']->query($sql);
	//print_r($res);
	return $res;
}
?>